import findIndex from 'lodash/findIndex';
import tool from './Tool';
import uniqBy from 'lodash/uniqBy';

/**
 * @desc Store 类  操作数据。目的是为了得到最新的 gridData 和 originData 和 图数据绑定
 */
class Store {
    /**
     * @desc 合并 originData 删除，或者添加， 删除的时候 nodes,links 传入id
     * @param {object} originData 原始数据
     * @param {object} newData 新载入的数据
     * @param {string} type add, del, update 增删改方法
     * @return nodes, links
     */
    _mergeOriginData(originData, newData, type) {
        console.log(originData, newData, type);
        let data = null;
        if (!originData) {
            return newData;
        }
        if (type === 'add') {
            let nodes = newData.nodes.concat(originData.nodes);
            let links = newData.links.concat(originData.links);

            nodes = uniqBy(nodes, 'id');
            links = uniqBy(links, 'id');

            data = { nodes, links };
        } else if (type === 'del') {
            let { nodes, links } = originData;
            nodes.forEach(node => {
                if (
                    findIndex(newData.nodes, d => {
                        return d === node.id;
                    }) !== -1
                ) {
                    node.remove = true;
                }
            });
            let nNodes = [];
            nodes.forEach(node => {
                if (node.remove === undefined) {
                    nNodes.push(node);
                }
            });
            links.forEach(link => {
                if (
                    findIndex(newData.links, d => {
                        return d === link.id;
                    }) !== -1
                ) {
                    link.remove = true;
                }
            });

            let nLinks = [];
            links.forEach(link => {
                if (link.remove === undefined) {
                    nLinks.push(link);
                }
            });

            data = { nodes: nNodes, links: nLinks };
        } else {
            let { nodes, links } = originData;

            nodes.forEach((node, i) => {
                let index = findIndex(newData.nodes, d => {
                    return d === node.id;
                });
                if (index !== -1) {
                    nodes[i] = newData.nodes[index];
                }
            });

            links.forEach((link, i) => {
                let index = findIndex(newData.links, d => {
                    return d === link.id;
                });
                if (index !== -1) {
                    links[i] = newData.links[index];
                }
            });

            data = { nodes, links };
        }
        return data;
    }

    // 改变originData, gridData
    _changeData(originData, data, type) {
        if (type !== 'reset') {
            originData = this._mergeOriginData(originData, data, type);
        }
        return originData;
    }

    // 数据过滤
    filterData(originData) {
        const { nodes, links } = originData;
        links.map(d => {
            d.id = tool.getRandomID();
            return d;
        });
        nodes.map(d => {
            d.key = tool.getRandomID();
            return d;
        });
        return { nodes, links };
    }

    // 差异数据
    diffOriginData(originData, data) {
        if (!originData) {
            return data;
        }

        let nodes = [];
        let links = [];

        data.nodes.forEach(node => {
            // 如果 data 里面的 node 的 id 在 originData 里面没有，说明是添加的
            if (
                findIndex(originData.nodes, d => {
                    return d.id === node.id;
                }) === -1
            ) {
                nodes.push(node.id);
            }
        });

        data.links.forEach(link => {
            // 如果 data 里面的 link 的 id 在 originData 里面没有，说明是添加的
            if (
                findIndex(originData.links, d => {
                    return d.id === link.id;
                }) === -1
            ) {
                links.push(link.id);
            }
        });

        return { nodes, links };
    }

    // 重新设置gridData
    resetGrid(originData) {
        return this._changeData(originData, null, 'reset');
    }

    // 添加数据
    add(originData, data) {
        return this._changeData(originData, data, 'add');
    }

    // 删除
    del(originData, data) {
        return this._changeData(originData, data, 'del');
    }

    // 更新数据
    update(originData, data) {
        return this._changeData(originData, data, 'update');
    }
}

export default new Store();
